package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	Connection conn;
	DefaultTableModel model;
	JTable table;
	int row;
	DatabaseTest d;

	private JLabel label;
	JTextField textCode;
	JTextField textEa;
	private JLabel label_1;
	private JButton btnNewButton;
	private JLabel labState;
	private JButton btnNewButton_1;
	JTextField textSerial;
	private JLabel label_2;

	/**
	 * Create the frame.
	 */
	
	public void setData(String a, String b, String c){
		textCode.setText(a);
		textEa.setText(b);
		textSerial.setText(c);
	}
	
	public Update(Connection conn,DatabaseTest d) {
		super("Update", false, true, false, false);// 두번째꺼 닫기창
		this.conn = conn;
		this.d=d;
		
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(0, 300, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getTextCode());
		getContentPane().add(getTextEa());
		getContentPane().add(getLabel_1());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLabState());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getTextSerial());
		getContentPane().add(getLabel_2());

	}
	
	public void setD(DefaultTableModel model,JTable table, int row){
		this.model=model;
		this.table=table;
		this.row=row;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setBounds(57, 67, 57, 15);
		}
		return label;
	}

	private JTextField getTextCode() {
		if (textCode == null) {
			textCode = new JTextField();
			textCode.setColumns(10);
			textCode.setBounds(123, 64, 116, 21);
		}
		return textCode;
	}

	private JTextField getTextEa() {
		if (textEa == null) {
			textEa = new JTextField();
			textEa.setColumns(10);
			textEa.setBounds(123, 95, 116, 21);
		}
		return textEa;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량");
			label_1.setHorizontalAlignment(SwingConstants.CENTER);
			label_1.setBounds(57, 98, 57, 15);
		}
		return label_1;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						String sql1 = "select * from PTbl where serial=?";
						PreparedStatement pstmt = conn.prepareStatement(sql1);

						pstmt.setInt(1, Integer.parseInt(textSerial.getText()));

						// 객체의 SQL 쿼리를 실행해, 그 쿼리에 의해 생성된 ResultSet 객체를 돌려줍니다.
						ResultSet rs = pstmt.executeQuery();
						
						if (rs.next()) {
								textCode.setText(rs.getString("code"));
								textEa.setText(rs.getInt("ea")+"");
								labState.setText("검색되었습니다.");
							}
						else {
							labState.setText("데이터가 없습니다.");
						}

						rs.close();
						pstmt.close();
					} catch (Exception ee) {

					}
				}
			});
			btnNewButton.setBounds(266, 40, 97, 23);
		}
		return btnNewButton;
	}

	private JLabel getLabState() {
		if (labState == null) {
			labState = new JLabel("");
			labState.setOpaque(true);
			labState.setBackground(Color.WHITE);
			labState.setBounds(58, 178, 325, 44);
		}
		return labState;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						String sql1 = "update ptbl set code=?, ea=? where serial=?";
						PreparedStatement pstmt = conn.prepareStatement(sql1);

						pstmt.setString(1, textCode.getText());
						pstmt.setInt(2, Integer.parseInt(textEa.getText()));
						pstmt.setInt(3, Integer.parseInt(textSerial.getText()));

						pstmt.executeUpdate();
						
						d.sel.QueryStart();
//						table.setValueAt(textCode.getText(), row, 0);
//						table.setValueAt(textEa.getText(), row, 1);
//						table.setValueAt(textSerial.getText(), row, 2);
						
						labState.setText("수정되었습니다.");
						textCode.setText("");
						textEa.setText("");
						textSerial.setText("");
				
						pstmt.close();
					} catch (Exception ee) {

					}
				}
			});
			btnNewButton_1.setBounds(123, 135, 116, 23);
		}
		return btnNewButton_1;
	}

	private JTextField getTextSerial() {
		if (textSerial == null) {
			textSerial = new JTextField();
			textSerial.setColumns(10);
			textSerial.setBounds(123, 36, 116, 21);
		}
		return textSerial;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("시리얼");
			label_2.setHorizontalAlignment(SwingConstants.CENTER);
			label_2.setBounds(57, 39, 57, 15);
		}
		return label_2;
	}
}
